package com.aistar.service.job.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.job.LabelTypeAndTag;
import com.aistar.model.dto.job.RecruitmentAndCompanyDTO;
import com.aistar.model.entity.*;
import com.aistar.service.job.IRecruitmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Transactional
@Service
public class RecruitmentServiceImpl implements IRecruitmentService {
    @Autowired
    private RecruitmentMapper recruitmentMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private LabelTypeMapper labelTypeMapper;
    @Autowired
    private RecruitmentLabelMapper recruitmentLabelMapper;
    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private JavaMailSender mailSender;


    @Override
    public List<Recruitment> search() {
        return recruitmentMapper.find();
    }

    @Override
    public Recruitment loadRecruitment(Integer id) {

        return recruitmentMapper.findById(id);
    }

    @Override
    public String titleName(Integer id) {
        return recruitmentMapper.findByName(id);
    }

    @Override
    public PageInfo<Recruitment> cutPage(String name, String experience, String education,Integer salaryLower, Integer salaryUpper,String province,String city,String district, Integer page, Integer limit,Integer tagId) {
        PageHelper.startPage(page, limit);
        if(province!=null||city!=null||district!=null){
            List<Integer> companyId=companyMapper.findByCity(province,city,district);
            List<Recruitment> r1=new ArrayList<>();
            for (Integer id : companyId) {
                List<Recruitment> recruitmentList=recruitmentMapper.findByCompanyId(id);
                for (Recruitment recruitment : recruitmentList) {
                    r1.add(recruitment);
                }
            }
            PageInfo<Recruitment> pageInfo = new PageInfo<>(r1);
            return pageInfo;
        } else if(tagId!=null){
            List<Integer> recruitmentLabelList=recruitmentLabelMapper.findByTagId(tagId);
            List<Recruitment> recruitmentList=new ArrayList<>();
            for (Integer id : recruitmentLabelList) {
                Recruitment recruitment=recruitmentMapper.findById(id);
                recruitmentList.add(recruitment);
            }
            PageInfo<Recruitment> pageInfo = new PageInfo<>(recruitmentList);
            return pageInfo;
        }else{
            List<Recruitment> recruitmentList = recruitmentMapper.getAll(name, experience, education,salaryLower,salaryUpper);
            List<Integer> salary=new ArrayList<>();
            for (Recruitment r : recruitmentList) {
                salary.add(r.getSalaryLower());
            }
            PageInfo<Recruitment> pageInfo = new PageInfo<>(recruitmentList);
            return pageInfo;
        }
    }

    @Override
    public List<RecruitmentAndCompanyDTO> searchAll() {
        List<RecruitmentAndCompanyDTO> recruitmentAndCompanyDTOList = new ArrayList<>();

        List<Recruitment> recruitmentList = recruitmentMapper.find();
        if (recruitmentList != null && recruitmentList.size() > 0) {
            for (Recruitment recruitment : recruitmentList) {
                RecruitmentAndCompanyDTO recruitmentAndCompanyDTO = new RecruitmentAndCompanyDTO();
                recruitmentAndCompanyDTO.setRecruitment(recruitment);
//                System.out.println(recruitmentAndCompanyDTO);
                Company company = companyMapper.findById(recruitment.getCompanyId());
                recruitmentAndCompanyDTO.setCompany(company);
                recruitmentAndCompanyDTOList.add(recruitmentAndCompanyDTO);
            }
        }
//        System.out.println(recruitmentAndCompanyDTOList.size());
        return recruitmentAndCompanyDTOList;
    }

    @Override
    public String searchWelfare(Integer id) {

        return recruitmentMapper.getWelfare(id);
    }

    @Override
    public Integer searchRecruitment(Integer id) {
        return recruitmentMapper.findByCompanyId(id).size();
    }

    @Override
    public List<Resume> findByRecruitmentId(Integer id) {
        return resumeMapper.findByEmployeeId(id);
    }

    @Override
    public Integer countEmployer(Integer id) {
        return recruitmentMapper.findCount(id);
    }

    @Override
    public List<Recruitment> searchByCompanyId(Integer id) {
        return recruitmentMapper.findByCompanyId(id);
    }

    @Override
    public List<LabelTypeAndTag> tagIdByType() {
        List<LabelTypeAndTag> labelTypeAndTagList = new ArrayList<>();

        List<LabelType> type = labelTypeMapper.findAll();
        for (LabelType labelType : type) {
            LabelTypeAndTag labelTypeAndTag = new LabelTypeAndTag();
            labelTypeAndTag.setLabelType(labelType);
            List<Tag> tag = tagMapper.findByType(labelType.getId());
            labelTypeAndTag.setTagList(tag);
            labelTypeAndTagList.add(labelTypeAndTag);
        }


        return labelTypeAndTagList;
    }

    @Override
    public int searchFrequency(Integer id) {
        return recruitmentMapper.findFrequency(id);
    }

    @Override
    public PageInfo<Recruitment> searchCidAndPage(Integer id, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Recruitment> recruitmentList = recruitmentMapper.findByCompanyId(id);
//        System.out.println(recruitmentList);
        PageInfo<Recruitment> pageInfo = new PageInfo<>(recruitmentList);
        return pageInfo;
    }


    //发送邮箱
    @Override
    public void SendEmail(Integer id,Integer rsid) {

        String email=recruitmentMapper.findByRId(id);

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setFrom("2294014200@qq.com");
        mailMessage.setSubject("轻聘-新消息");
        String mail = "您有一份简历等待查收"+resumeMapper.findByIdToAvatar(rsid);
        mailMessage.setText(mail);
        mailMessage.setTo(email);

        mailSender.send(mailMessage);

    }

    @Override
    public List<Recruitment> searchLower(String name, String experience, String education,@RequestParam(defaultValue = "3")Integer salaryLower,@RequestParam(defaultValue = "20") Integer salaryUpper) {
        if(salaryLower==0&&salaryUpper==0){
            return null;
        }
        List<Recruitment> lowerList =recruitmentMapper.getAll(name,experience,education,salaryLower,salaryUpper);
        List<Recruitment> recruitmentList=new ArrayList<>();
        for (Recruitment recruitment : lowerList) {
            if(recruitment.getSalaryLower()>=salaryLower&&recruitment.getSalaryUpper()<=salaryUpper){
//                System.out.println(recruitment);
                recruitmentList.add(recruitment);
            }
        }
//        System.out.println(recruitmentList);
        return recruitmentList;
    }

    @Override
    public List<String> findWelfareByRecruitmentId(Integer recruitmentId) {
        // 根据招聘者id获取薪资福利
        String wel = recruitmentMapper.getWelfare(recruitmentId);
        // 分割
        String[] welArr = wel.split(",");
        // 遍历存放到集合中
        List<String> welList = new ArrayList<>();
        for (String s : welArr) {
            welList.add(s);
        }
        return welList;
    }
}
